home *** CD-ROM | disk | FTP | other *** search
/ PC Answers 1995 May / PC Answers CD-ROM 7 (Future Publishing) (May 1995).iso / vbits / code / pleas / ole / visio / network / visreg.bas < prev    next >
Encoding:
BASIC Source File  |  1994-05-18  |  14.9 KB  |  441 lines

  1. '------------------------------------------------------------------------------
  2. '------------------------------------------------------------------------------
  3. '--
  4. '--                   Visio Instance Registration Utilites
  5. '--                       (C)1993 Shapeware Corporation
  6. '--
  7. '--   File Name : visreg.bas
  8. '--
  9. '-- Description : Contains helper functions for working with Visio instances.
  10. '--               To use this module include it into your project and use one
  11. '--               of the three levels available.  For more information see
  12. '--               below.
  13. '--
  14. '--               The registration utility offers an easy way get and create
  15. '--               Visio instance objects.  It offers three levels of instancing
  16. '--               from simple get/create/release to registration where the
  17. '--               library maintains the "signature" of a Visio instance and
  18. '--               warns you when the active instance changes.
  19. '--
  20. '--               The library maintains a static global g_appVisio which
  21. '--               is called the global instance object (GIO).  Use GIO in
  22. '--               your code when refering to the working instance of Visio.
  23. '--               Never apply the Set operator to GIO yourself (unless you
  24. '--               really know what your doing).
  25. '--
  26. '--               To use the library, include it in your project and refer the
  27. '--               visio application object through g_appVisio (GIO).
  28. '--               Read the sections below to find the level of functionality
  29. '--               you want.
  30. '--
  31. '--               Low Level Routines
  32. '--
  33. '--               The low level routines are almost identical to what you
  34. '--               would use normally with GetObject and CreateObject.
  35. '--               However they encapsulate the error handling.
  36. '--
  37. '--                   vaoGetGIO()     Retrieves active, running instances.
  38. '--                   vaoCreateGIO()  Creates a new instance.
  39. '--                   vaoReleaseGIO() Release the GIO instance if Set.
  40. '--                   vaoIsGIOValid() Verifies that GIO is Set and loaded.
  41. '--
  42. '--               Registration/Release Level
  43. '--
  44. '--                   Use this level when you need the registration functions
  45. '--                   to maintain the GIO but want control over how it is
  46. '--                   obtained.  The procedures are:
  47. '--
  48. '--                       vaoRegisterGIO()
  49. '--                       vaoUnRegistrGIO()
  50. '--                       vaoReSetGIO()
  51. '--                       (vaoReleaseGIO()    [From low level])
  52. '--
  53. '--                   To begin you register your instance and choose how to
  54. '--                   retrieve the GIO (Get/Create/Both) with
  55. '--                   vaoRegisterGIO() and use vaoUnRegisterGIO to release
  56. '--                   it.  This gives a good amount of flexibility but leaves
  57. '--                   it up to you to handle the conditions where Visio is shut
  58. '--                   down or a new instance is loaded.  At this level you keep
  59. '--                   the instance registered but release the GIO using
  60. '--                   vaoReleaseGIO.  To get back GIO use vaoReSetGIO.
  61. '--
  62. '--               Most Common Level
  63. '--
  64. '--                   This is highest, most abstract level.  It's called Most
  65. '--                   Common level because most scripts will probably use it
  66. '--                   to get instance objects.  There is one function:
  67. '--
  68. '--                       vaoGetObject()
  69. '--
  70. '--                   When called it will check to see if the GIO is already
  71. '--                   registered.  If not it will first attempt a GetObject
  72. '--                   and, if that fails, will use CreateObject.  Unless Visio
  73. '--                   is not installed, you will get visOK back.  On subsequent
  74. '--                   calls it checks that it is still valid (not UnSet and
  75. '--                   still running).  If so it returns visOK, otherwise it
  76. '--                   tries to register the GIO again.  If that fails you
  77. '--                   receive visError.  The nice thing about this is that one
  78. '--                   call maintains the GIO for you.
  79. '--
  80. '------------------------------------------------------------------------------
  81. '------------------------------------------------------------------------------
  82.  
  83. Option Explicit                                 '-- All Variable Explicit!
  84.  
  85. Global g_appVisio As object
  86.  
  87. Const REG_GET_HWND = 1
  88. Const REG_SET_HWND = 2
  89.  
  90. Global Const visDiffInst = 1
  91. Global Const visGet = 2
  92. Global Const visCreate = 3
  93. Global Const visVisioQuit = 4
  94. Global Const visError = 5
  95. Global Const visRegistered = 6
  96. Global Const visOK = 7
  97.  
  98. Private Function GetHWND ()
  99. '-------------------------------
  100. '--- GetHWND -------------------
  101. '--
  102. '--   Returns the registered Visio Window Handle.
  103. '--
  104.  
  105.     Dim iTemp As Integer
  106.  
  107.     VisWindowHandle REG_GET_HWND, iTemp
  108.  
  109.     GetHWND = iTemp
  110. End Function
  111.  
  112. Private Function Registered () As Integer
  113. '-------------------------------
  114. '--- RegisterVisio -------------
  115. '--
  116. '--   Returns boolean integer indicating if we are registered or not.
  117. '--
  118.  
  119.     Registered = (GetHWND() <> 0)
  120. End Function
  121.  
  122. Private Sub SetHWND (ByVal iNewHWND As Integer)
  123. '-------------------------------
  124. '--- SetHWND -------------------
  125. '--
  126. '--   Sets the registered Visio Window Handle.
  127. '--
  128.  
  129.     VisWindowHandle REG_SET_HWND, iNewHWND
  130. End Sub
  131.  
  132. Function vaoCreateGIO () As Integer
  133. '-------------------------------
  134. '--- vaoCreateGIO --------------
  135. '--
  136. '--   Uses CreateObject to create a new instance of Visio.  If it fails
  137. '-- False is returned, otherwise the GIO is set to the instance created
  138. '-- and True is returned.
  139. '--
  140.  
  141.     On Error GoTo vaoCreateGIOErrorHandler
  142.  
  143.     Debug.Print "VISREG.BAS vaoCreateGIO() - Creating new Visio instance."
  144.  
  145.     Set g_appVisio = CreateObject("visio.application")
  146.  
  147.     If Not (g_appVisio Is Nothing) Then
  148.     vaoCreateGIO = True
  149.     End If
  150.  
  151.     Exit Function
  152.  
  153. vaoCreateGIOErrorHandler:
  154.     Debug.Print "VISREG.BAS vaoCreateGIO() - Failed."
  155.     Exit Function
  156.     Resume Next
  157. End Function
  158.  
  159. Function vaoGetGIO () As Integer
  160. '-------------------------------
  161. '--- vaoGetGIO -----------------
  162. '--
  163. '--   Uses GetObject to get the active instance of Visio.  If GetObject fails
  164. '-- False is returned, otherwise the GIO is set and True is returned.
  165. '--
  166.  
  167.     On Error GoTo vaoGetErrorHandler
  168.  
  169.     Debug.Print "VISREG.BAS vaoGetGIO() - Retrieving active Visio instance."
  170.  
  171.     Set g_appVisio = GetObject(, "visio.application")
  172.  
  173.     If Not (g_appVisio Is Nothing) Then
  174.     vaoGetGIO = True
  175.     End If
  176.     
  177.     Exit Function
  178.  
  179. vaoGetErrorHandler:
  180.     Debug.Print "VISREG.BAS vaoGetGIO() - Failed."
  181.     Exit Function
  182.     Resume Next
  183. End Function
  184.  
  185. Function vaoGetObject () As Integer
  186. '-------------------------------
  187. '--- vaoGetObject --------------
  188. '--
  189. '--   Uses registration procedures to maintain the GIO.  This funciton makes
  190. '-- up the Common Use Layer (most commonly used procedure) for using the GIO.
  191. '-- Just call it every time you need to work with Visio and it will make sure
  192. '-- you have a valid working copy.
  193. '--
  194. '-- Return Values:
  195. '--  visOK        - The GIO is set to a valid working instance of Visio.
  196. '--  visError     - Visio 2.0 or OLE not installed or some other serious
  197. '--                 error occurred.
  198. '--
  199.  
  200.     Dim iRetVal As Integer, iTemp As Integer, l_appVisio As object
  201.     
  202.     iRetVal = visOK                             '-- Default To OK
  203.  
  204.     If Registered() Then                        '-- When Registerd...
  205.     If Not vaoIsGIOValid() Then             '--   If GIO Is Valid...
  206.         Debug.Print "VISREG.BAS vaoGetObject() - Re-registering instance."
  207.  
  208.         '-- Somehow the GIO is no longer valid, either because it was
  209.         '-- vaoReleaseGIO'd or is no longer running.  Therefore we just
  210.         '-- try to re-register and if the same instance is active, we
  211.         '-- get that one again.  Otherwise we end up with the active
  212.         '-- instance of Visio or a newly created one.
  213.         '--
  214.         '-- In future versions of Visio we will iterate through the
  215.         '-- instance collection and retrieve the instance we originally
  216.         '-- registered to if it still exists.
  217.  
  218.         vaoUnRegisterGIO                    '--   Oops, Its Bad Now...
  219.  
  220.         If vaoRegisterGIO(True, True) = visError Then
  221.         iRetVal = visError
  222.         End If
  223.     End If
  224.     Else
  225.     If vaoRegisterGIO(True, True) = visError Then iRetVal = visError
  226.     End If
  227.  
  228.     vaoGetObject = iRetVal
  229. End Function
  230.  
  231. Function vaoGetVisio (bGet As Integer, bCreate As Integer) As Integer
  232. '-------------------------------
  233. '--- vaoGetVisio ---------------
  234. '--
  235. '--   Identical to vaoRegisterGIO except doesn't use registration functions.
  236. '--
  237. '-- Parameters : bUseExisting - Boolean - Use vaoGetGIO() first.
  238. '--              bCreate      - Boolean - Use vaoCreateGIO().
  239. '--
  240. '--    Returns : visError     - If an error occurred and the GIO could not be
  241. '--                             set.  Either the flags were invalid or
  242. '--                             Get & Create failed.
  243. '--              visGet       - When a vaoGetGIO() retrieved the GIO.
  244. '--              visCreate    - When a vaoCreateGIO() retrieved the GIO.
  245. '--              visRegisterd - Failed - GIO is registered.  Use
  246. '--                             vaoUnRegisterGIO().
  247. '--
  248.  
  249.     Dim iRetVal As Integer
  250.  
  251.     ' If registered we fail.
  252.     '
  253.     If Registered() Then
  254.     iRetVal = visRegistered
  255.     GoTo lblGetVisioCleanUp
  256.     End If
  257.  
  258.     iRetVal = visError
  259.     
  260.     ' If the Get flag was set we first try vaoGetGIO()
  261.     '
  262.     If bGet Then
  263.     If vaoGetGIO() Then iRetVal = visGet
  264.     End If
  265.  
  266.     ' If the Create flag is on and the return value doesn't indicate that
  267.     ' a get worked then we use create.
  268.     '
  269.     If bCreate And (iRetVal <> visGet) Then
  270.     If vaoCreateGIO() Then iRetVal = visCreate
  271.     End If
  272.  
  273.     ' If the GIO isn't set at this point we output an error message.
  274.     '
  275.     If g_appVisio Is Nothing Then
  276.     Debug.Print "VISREG.BAS vaoRegisterGIO() - Error registering GIO."
  277.     End If
  278.         
  279. lblGetVisioCleanUp:
  280.     vaoGetVisio = iRetVal
  281. End Function
  282.  
  283. Function vaoIsGIOValid () As Integer
  284. '-------------------------------
  285. '--- vaoIsGIOValid -------------
  286. '--
  287. '--   Our validity test simply checks to see if the GIO is set and, if so,
  288. '-- checks if it is loaded.
  289. '--
  290. '-- Returns : True if GIO is set and loaded, False otherwise.
  291. '--
  292.     On Error GoTo lblvaoGIOValidErr
  293.  
  294.     Dim iTemp As Integer
  295.  
  296.     vaoIsGIOValid = False                           '-- Default To False
  297.  
  298.     If (g_appVisio Is Nothing) Then Exit Function   '-- Not Set
  299.  
  300.     iTemp = g_appVisio.Documents.Count              '-- Try A Property
  301.  
  302.     vaoIsGIOValid = True                            '-- No Error - Valid!
  303.     Exit Function
  304.  
  305. lblvaoGIOValidErr:
  306.     Exit Function                                   '-- Error - Invalid
  307.     Resume Next
  308. End Function
  309.  
  310. Function vaoRegisterGIO (bUseExisting As Integer, bCreate As Integer) As Integer
  311. '-------------------------------
  312. '--- vaoRegisterGIO ------------
  313. '--
  314. '--   Registers the GIO using two parameters to decide how the Visio instance
  315. '-- should be created.  Use vaoUnRegisterGIO to reverse the registration.
  316. '--
  317. '-- Parameters : bUseExisting - If True then GetObject will tried first.
  318. '--              bCreate      - If True CreateObject will be called after
  319. '--                             any GetObject calls.
  320. '--
  321. '--    Returns : visError     - If an error occurred and the GIO could not be
  322. '--                             registered because either the flags passed
  323. '--                             were invalid or Get & Create failed.
  324. '--              visGet       - When a GetObject retrieved the GIO.
  325. '--              visCreate    - When a CreateObject retrieved the GIO.
  326. '--              visRegisterd - When already registered.
  327. '--
  328.  
  329.     Dim iRetVal As Integer
  330.  
  331.     If Registered() Then
  332.     iRetVal = visRegistered
  333.     GoTo lblRegisterCleanUp
  334.     End If
  335.  
  336.     iRetVal = visError
  337.  
  338.     If bUseExisting Then
  339.     If vaoGetGIO() Then iRetVal = visGet
  340.     End If
  341.  
  342.     If bCreate And (iRetVal <> visGet) Then
  343.     If vaoCreateGIO() Then iRetVal = visCreate
  344.     End If
  345.  
  346.     If g_appVisio Is Nothing Then
  347.     Debug.Print "VISREG.BAS vaoRegisterGIO() - Error registering GIO."
  348.     Else
  349.     SetHWND g_appVisio.WindowHandle
  350.     End If
  351.  
  352. lblRegisterCleanUp:
  353.     vaoRegisterGIO = iRetVal
  354. End Function
  355.  
  356. Sub vaoReleaseGIO ()
  357. '-------------------------------
  358. '--- vaoReleaseGIO -------------
  359. '--
  360. '--   Handles releasing the GIO.  Does not unregister the window handle.
  361. '-- If using the registration interfaces use vaoReSetGIO to retrieve the
  362. '-- GIO, otherwise you may use vaoGetGIO or vaoCreateGIO.  This does not
  363. '-- take affect until all other references go out of scope.
  364. '--
  365.  
  366.     Set g_appVisio = Nothing                '-- Release Resources
  367.  
  368.     Debug.Print "VISREG.BAS vaoReleaseGIO() - Complete."
  369. End Sub
  370.  
  371. Function vaoReSetGIO () As Integer
  372. '-------------------------------
  373. '--- vaoReSetGIO ---------------
  374. '--
  375. '--   Tries to re-Set the GIO only if we are registered and the GIO is not
  376. '-- already set.
  377. '--
  378. '-- Return Values :
  379. '--   visError     - If not registered or GIO is already set.
  380. '--   visOK        - If able to reSet the GIO to the registered instance.
  381. '--   visVisioQuit - If Visio is no longer running.  If so then the GIO is
  382. '--                  unregistered because the HWND is no longer valid.
  383. '--   visDiffInst  - If the registered instance is no longer running.  The
  384. '--                  GIO is not set.
  385. '--
  386.  
  387.     vaoReSetGIO = visError
  388.     
  389.     If Not Registered() Or Not (g_appVisio Is Nothing) Then Exit Function
  390.  
  391.     If vaoGetGIO() Then
  392.     If g_appVisio.WindowHandle = GetHWND() Then
  393.         vaoReSetGIO = visOK
  394.     Else
  395.         vaoReleaseGIO
  396.         vaoReSetGIO = visDiffInst       '-- Release GIO
  397.     End If
  398.     Else
  399.     vaoReSetGIO = visVisioQuit
  400.     vaoUnRegisterGIO                    '-- UnRegister
  401.     End If
  402. End Function
  403.  
  404. Sub vaoUnRegisterGIO ()
  405. '-------------------------------
  406. '--- vaoUnRegisterGIO ----------
  407. '--
  408. '--   Unregisters a visio instance by clearing the window handle and releasing
  409. '-- the global instance object.
  410. '--
  411.     
  412.     SetHWND 0                           '-- Resets HWND
  413.     vaoReleaseGIO                       '-- Releases GIO
  414.     
  415.     Debug.Print "VISREG.BAS vaoUnRegisterGIO() - Completed."
  416. End Sub
  417.  
  418. Private Sub VisWindowHandle (ByVal iAction As Integer, iArg As Integer)
  419. '-------------------------------
  420. '--- VisWindowHandle -----------
  421. '--
  422. '--   Maintains the registered window handle in a static variable.
  423. '--
  424. '-- Parameters : iAction - Specifies the action to perform.  REG_GET_HWND
  425. '--                        sets iArg to the handle.  REG_SET_HWND sets the
  426. '--                        handle to iArg.
  427. '--
  428. '--              iArg    - Used in gets/sets.
  429. '--
  430.  
  431.     Static iHWND As Integer
  432.  
  433.     Select Case iAction
  434.     Case REG_GET_HWND: iArg = iHWND
  435.     Case REG_SET_HWND: iHWND = iArg
  436.     Case Else:
  437.         Debug.Print "VISREG.BAS VisWindowHandle() - Invalid Action Passed"
  438.     End Select
  439. End Sub
  440.  
  441.